package com.cloud.controller;

import com.cloud.service.FeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@DefaultProperties(defaultFallback = "order_Global_Info")
public class FeignHytrixController {



    @Autowired
    private FeignService service;

    @GetMapping("/order/hystrix/ok/{id}")
    @HystrixCommand
    public String paymentInfo(@PathVariable("id") Integer id){
        int i=1/0;
        return service.paymentInfo(id);
    }

    @HystrixCommand(fallbackMethod = "paymentInfo_timeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value="3000")
    })
    @GetMapping("/order/hystrix/timeout/{id}")
    public String paymentInfo_timeOut(@PathVariable("id")Integer id) {
        try{
            TimeUnit.SECONDS.sleep(4);
        }catch (Exception e){
            e.printStackTrace();
        }
        return service.paymentInfo_timeOut(id);

    }
    public String paymentInfo_timeOutHandler(Integer id){
        return "线程池："+Thread.currentThread().getName()+" paymentInfo_timeOutHandler,id："+id+"\t"+"哈哈哈"+"超时了了";
    }

    //下面是全局fallbakc
    public String order_Global_Info(){
        return "服务调用时错误，请稍后再试";
    }

}
